import request from '@/utils/request'
import { ApiResponse, IdResponse } from '@/types/api'

// 定义客户相关类型
interface Customer {
  id: number
  name: string
  address: string
  tax_num: string
  tax_bank: string
  tax_banksn: string
  tax_mobile: string
  tax_address: string
  contact_name?: string
  contact_mobile?: string
  belong_uid: number
  belong_did: number
  is_lock: number
  create_time: string
  update_time: string
  delete_time: number
}

// 客户联系人
interface CustomerContact {
  id: number
  cid: number
  name: string
  mobile: string
  sex: number
  qq: string
  wechat: string
  email: string
  is_default: number
  create_time: string
  admin_id: number
}

// 客户跟进记录
interface CustomerTrace {
  id: number
  cid: number
  content: string
  file_ids: string
  create_time: string
  admin_id: number
}

// 销售机会
interface CustomerChance {
  id: number
  cid: number
  name: string
  amount: number
  grade_id: number
  source_id: number
  status: number
  remark: string
  create_time: string
  update_time: string
  admin_id: number
}

// 获取客户列表
export function getCustomerList(params: any): Promise<ApiResponse<Customer[]>> {
  return request({
    url: '/api/customer/customer/list',
    method: 'get',
    params,
  })
}

// 获取客户详情
export function getCustomerDetail(id: number): Promise<ApiResponse<Customer>> {
  return request({
    url: '/api/customer/customer/view',
    method: 'get',
    params: { id },
  })
}

// 新增客户
export function addCustomer(data: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/customer/add',
    method: 'post',
    data,
  })
}

// 编辑客户
export function editCustomer(data: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/customer/edit',
    method: 'post',
    data,
  })
}

// 删除客户
export function deleteCustomer(id: number): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/customer/del',
    method: 'delete',
    data: { id },
  })
}

// 锁定/解锁客户
export function lockCustomer(data: { id: number; is_lock: number }): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/customer/lock',
    method: 'post',
    data,
  })
}

// 分配客户
export function distributeCustomer(data: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/customer/distribute',
    method: 'post',
    data,
  })
}

// 获取客户联系人列表
export function getCustomerContacts(params: {
  cid: number
}): Promise<ApiResponse<CustomerContact[]>> {
  return request({
    url: '/api/customer/contact/list',
    method: 'get',
    params,
  })
}

// 新增客户联系人
export function addCustomerContact(data: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/contact/add',
    method: 'post',
    data,
  })
}

// 编辑客户联系人
export function editCustomerContact(data: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/contact/edit',
    method: 'post',
    data,
  })
}

// 删除客户联系人
export function deleteCustomerContact(id: number): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/contact/del',
    method: 'delete',
    data: { id },
  })
}

// 设置默认联系人
export function setDefaultContact(data: { id: number; cid: number }): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/contact/set_default',
    method: 'post',
    data,
  })
}

// 获取客户跟进记录
export function getCustomerTraces(params: { cid: number }): Promise<ApiResponse<CustomerTrace[]>> {
  return request({
    url: '/api/customer/trace/list',
    method: 'get',
    params,
  })
}

// 新增客户跟进记录
export function addCustomerTrace(data: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/trace/add',
    method: 'post',
    data,
  })
}

// 获取销售机会列表
export function getCustomerChances(params: {
  cid: number
}): Promise<ApiResponse<CustomerChance[]>> {
  return request({
    url: '/api/customer/chance/list',
    method: 'get',
    params,
  })
}

// 新增销售机会
export function addCustomerChance(data: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/chance/add',
    method: 'post',
    data,
  })
}

// 编辑销售机会
export function editCustomerChance(data: any): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/chance/edit',
    method: 'post',
    data,
  })
}

// 删除销售机会
export function deleteCustomerChance(id: number): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/chance/del',
    method: 'delete',
    data: { id },
  })
}

// 删除客户跟进记录
export function deleteCustomerTrace(id: number): Promise<ApiResponse<any>> {
  return request({
    url: '/api/customer/trace/del',
    method: 'delete',
    data: { id },
  })
}

// 获取废弃客户列表
export function getDiscardCustomerList(
  params: any
): Promise<
  ApiResponse<{ list: Customer[]; total: number; page: number; limit: number; pages: number }>
> {
  return request({
    url: '/api/customer/customer/list',
    method: 'get',
    params,
  })
}
